package com.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.constant.ActivationConstant;
import com.example.common.constant.RedisConstant;
import com.example.common.redis.RedisUtils;
import com.example.common.utils.StringUtils;
import com.example.system.domain.Process;
import com.example.system.domain.User;
import com.example.system.domain.bo.ProcessBo;
import com.example.system.domain.vo.UserVo;
import com.example.system.mapper.ProcessMapper;
import com.example.system.mapper.UserMapper;
import com.example.system.service.ActivationService;
import com.example.system.service.DictService;
import com.example.system.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DictService diceService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ActivationService activationService;

    @Override
    public List<Integer> getOneProjectShareUser(Integer projectId) {
        List<Process> process = processMapper.getProcessListByProjectId(projectId);
        List<Integer> userIds = process.stream().map(Process::getUserId).collect(Collectors.toList());
        List<Integer> list = userMapper.selectList(new LambdaQueryWrapper<>()).stream().map(User::getUserId).filter(userIds::contains).collect(Collectors.toList());
        return list;
    }

    @Override
    public void createShareUserRedis() {
        List<UserVo> userList = userMapper.getUserListVo();
        userList = userList.stream().peek(o -> o.setImage("")).filter(o -> o.getAccess() != null && o.getAccess() <= 5).collect(Collectors.toList());
        long time = RedisConstant.getList_ShareUser_Time;
        String key = RedisConstant.getList_ShareUser;
        redisUtils.set(key, userList, time);
    }

    @Override
    public void updateProcess(ProcessBo processBo) {
        if ("NO".equals(processMapper.selectById(processBo.getId()).getIsProcess())) {
            activationService.updateActivation(processBo.getUserId(), ActivationConstant.Process);
        }
        processBo.setIsProcess("YES");
        processMapper.updateProcess(processBo);
    }

    @Override
    public List<Map<String, Object>> getAllShareUser() {
        String key = RedisConstant.getList_ShareUser;
        List<Map<String, Object>> list = ((List<UserVo>) redisUtils.get(key)).stream().map(o -> {
            Map<String, Object> map = new HashMap<>();
            map.put("userId", o.getUserId());
            map.put("username", o.getUsername());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public void changeUP(List<Integer> userIdS, Integer projectId) {
        List<Process> process = processMapper.getProcessListByProjectId(projectId);
        List<Integer> userId = process.stream().map(Process::getUserId).collect(Collectors.toList());
        List<Integer> delete = userId.stream().filter(o -> !userIdS.contains(o)).collect(Collectors.toList());
        List<Integer> insert = userIdS.stream().filter(o -> !userId.contains(o)).collect(Collectors.toList());
        List<Process> deleteList = new ArrayList<>();
        if (StringUtils.isNotEmpty(delete)) {
            for (Integer user_id : delete)
                deleteList.add(Process.newProcess(null, projectId, user_id));
            processMapper.deleteList(deleteList);
        }
        List<Process> insertList = new ArrayList<>();
        if (StringUtils.isNotEmpty(insert)) {
            for (Integer user_id : insert)
                insertList.add(Process.newProcess(0, projectId, user_id));
            processMapper.insertProcessList(insertList);
        }
    }


}
